"use strict";
import util from './util.js'
export default {
	/*缓存key*/
	_storageKey: 'sys_storage_key',
	/*默认配置*/
	_default: {
		updateServer: 'http://www.yilunyun.com:8080/', //服务器地址
		updateService: "smartPhone/getAppVersionInfo", //检测方法
		apiUrl: 'http://iot.hejidata.com', //api http://iot.hejidata.com 地址 http://139.224.13.75 
		// apiUrl: 'http://192.168.2.134', //api 地址 http://139.224.13.75 
		apiPort: '1180', // 18080 18070 18050
		apiUrlWeb: '',
		apiPortWeb: '',
		sysVersion: '1.0.0.0', //当前系统版本
		appId: '10', //01：空气，02：SMART，03：维护  04：AirKeep 05用电app 06多版本智量仪 07空气狗
		userId: '', //登陆用户
		fsiteNo: '', // 客户编号
		userName: '', //用户名
		froleid: '', // 角色id 管理员1 普通用户2
		froleidName: '', // 角色名称
		isAuto: false, // 是否自动登录
		userPwd: '', // 用户密码
		token: '', // 用户token
		langKey:'en-US',//语言环境
		fsiteName: '', //客户名称
		labName: '', // 实验室名称
		labNo: '', // 实验室编号
		buildingName: '', // 实验楼名称
		buildingNo: '', // 实验楼编号
	},
	/*获取默认*/
	getDefault: function() {
		this.setSysInfo(); //缓存默认
		return this._default;
	},
	/*获取系统信息*/
	getSysInfo: function() {
		try {
			let sys = uni.getStorageSync(this._storageKey);
			if (sys == null || sys === '') sys = this.getDefault();
			return sys;
		} catch (e) {}
	},

	/*获取api调用地址*/
	getApiUrl: function() {
		let {
			apiUrl,
			apiPort
		} = this.getSysInfo();
		return `${apiUrl}:${apiPort}/`;
	},
	
	/*获取websocketapi调用地址*/
	getWebApiUrl: function() {
		let {
			apiUrlWeb,
			apiPortWeb
		} = this.getSysInfo();
		return `ws://${apiUrlWeb}:${apiPortWeb}`;
	},

	/*获取系统版本号*/
	getVersion: function() {
		return this.getSysInfo().sysVersion;
	},
	/*设置系统版本号*/
	setVersion: function(server) {
		let sys = this.getSysInfo();
		sys = sys || {};
		sys.sysVersion = server.verson;
		this.setSysInfo(sys);
	},

	/*设置服务器信息*/
	setServer: function(server) {
		let sys = this.getSysInfo();
		sys = sys || {};
		sys.apiUrl = server.url;
		sys.apiPort = server.port;
		sys.apiUrlWeb = server.urlWeb
		sys.apiPortWeb = server.portWeb
		this.setSysInfo(sys);
	},

	/**
	 * 设置用户信息
	 */
	setUser: function(user) {
		console.log('user',user)
		let sys = this.getSysInfo();
		sys = sys || {};  
		sys.userId = user.userId;
		sys.token = user.token;
		sys.userName = user.name;
		sys.froleid = user.froleid;
		sys.fsiteNo = user.fsiteNo
		sys.fsiteName = user.fsiteName
		sys.isAuto = user.isAuto
		sys.labName = user.labName;
		sys.labNo = user.labNo;
		sys.buildingName = user.buildingName;
		sys.buildingNo = user.buildingNo;
		sys.froleidName = user.froleidName;
		if(user.isAuto){
			sys.userPwd = user.userPwd
		}else{
			sys.userPwd = ''
		}
		this.setSysInfo(sys);
		return sys;
	},

	/*设置系统信息*/
	setSysInfo: function(opt) {
		try {
			opt = opt || {};
			let opts = Object.assign({}, this._default, opt);
			uni.setStorageSync(this._storageKey, opts);
		} catch (e) {
			util.showToast(e);
		}
	},

	/*移除缓存*/
	removeSysImfo: function() {
		try {
			uni.removeStorageSync(this._storageKey);
		} catch (e) {
			// error
		}
	},
}
